package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.CouponService;

public class CouponAction extends AppBaseAction{
	
	@Resource(name = CouponService.BEAN_NAME)
	CouponService couponService;
	
	/**
	 * 统计用户使用的红包
	 * @return
	 */
	public String countCoupons(){
		JSONObject jsonObject = new JSONObject();
		try {
			String userId = this.getUserId();
			this.validateSign(getLoginSign(),userId);
			this.validateLoginApp();//验证是否登录
			
			jsonObject = this.couponService.countCoupons(userId);
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 获取优惠券列表
	 * @return
	 */
	public String getCouponList(){
        
		JSONObject jsonObject = new JSONObject();
		try {
			String type = this.request.getParameter("type");//1未用2已使用
			String userId = this.getUserId();
			this.validateSign(getLoginSign(),userId);
			this.validateLoginApp();//验证是否登录
			
			String pageSize = request.getParameter("pageSize");
			String pageNumber = request.getParameter("pageNumber");
			
			try {
				this.page.setPageSize(Integer.parseInt(pageSize));
			} catch (Exception e) {
			}
			try {
				this.page.setPageNumber(Integer.parseInt(pageNumber));
			} catch (Exception e) {
			}
			
			jsonObject = this.couponService.getCouponList(userId,type,page);
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
